﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FarmacieDAL;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Farmacie.Controls
{
    /// <summary>
    /// Interaction logic for InfoChitanta.xaml
    /// </summary>
    public partial class InfoChitanta : UserControl, INotifyPropertyChanged
    {
        public ObservableCollection<MedicamentVandut> MedicamenteVandute {get; private set;}
        public ObservableCollection<string> SubstanteActivePeReteta { get; private set; }

        public InfoChitanta()
        {
            InitializeComponent();
            MedicamenteVandute = new ObservableCollection<MedicamentVandut>();
            SubstanteActivePeReteta = new ObservableCollection<string>();

            radGridView_detaliiChitanta.ItemsSource = MedicamenteVandute;
            listaSubstante.ItemsSource = SubstanteActivePeReteta;

            this.DataContextChanged += new DependencyPropertyChangedEventHandler(InfoChitanta_DataContextChanged);
        }

        void InfoChitanta_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            //Data contextul ar trebui sa fie un obiect de tip chitanta
            if (!(this.DataContext is Chitanta)) return;

            Chitanta chitantaCurenta = this.DataContext as Chitanta;

            InitializeData(chitantaCurenta);

            if (chitantaCurenta.Reteta != null)
            {
                detaliiReteta.Visibility = System.Windows.Visibility.Visible;
                noReteta.Visibility = System.Windows.Visibility.Collapsed;

                detaliiReteta.DataContext = chitantaCurenta.Reteta;
            }
            else
            {
                detaliiReteta.Visibility = System.Windows.Visibility.Collapsed;
                noReteta.Visibility = System.Windows.Visibility.Visible;
            }
        }

        void InitializeData(Chitanta chitanta)
        {
            MedicamenteVandute.Clear();
            List<Vanzare> vanzari = ChitanteManager.GetVanzariByChitanta(chitanta);

            if (vanzari == null) return;

            foreach (Vanzare vanzare in vanzari)
                MedicamenteVandute.Add(new MedicamentVandut(vanzare));

            SubstanteActivePeReteta.Clear();

            if (chitanta.Reteta != null)
            {
                List<Substanta_Activa> substanteReteta = RetetaManager.GetSubstanteActiveByReteta(chitanta.Reteta);

                substanteReteta.ForEach(substanta => SubstanteActivePeReteta.Add(substanta.SubstantaActiva));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

    public class MedicamentVandut
    {
        public string Nume { get; private set; }
        public double PretUnitate { get; private set; }
        public double Cantitate { get; private set; }
        public double PretTotal { get; private set; }

        public MedicamentVandut(Vanzare vanzare)
        {
            Nume = vanzare.Medicament.NumeMedicament;
            PretUnitate = vanzare.Pret;
            Cantitate = vanzare.Cantitate;
            PretTotal = vanzare.Total;
        }
    }
}
